package main

import (
	"ZxDict/pkg/util"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/go-vgo/robotgo"
	"log"
	"os"
	"syscall"
	"time"
	"unsafe"
)

func setFont() {
	err := os.Setenv("FYNE_FONT", "C:\\Windows\\Fonts\\STSONG.TTF")
	if err != nil {
		// 错误处理逻辑
	}
}

func main() {
	//setFont()

	myApp := app.NewWithID("990419")
	myApp.Settings().SetTheme(&util.BasicTheme{})
	myWindow := myApp.NewWindow("Z-Tool")
	// 创建标签页容器
	tabs := container.NewAppTabs(
		getTranslatorApp(myApp),
	)

	tabs.SetTabLocation(container.TabLocationLeading)

	// 设置默认选中标签页（索引从0开始）
	tabs.SelectIndex(0)
	myWindow.SetContent(tabs)

	width, height := robotgo.GetScreenSize()
	windowsWidth := float32(width) * 0.382
	windowHeight := float32(height) * 0.382
	log.Println(width, height)

	myWindow.Resize(fyne.NewSize(windowsWidth, windowHeight))
	myWindow.CenterOnScreen()
	// 监听 Canvas 内容变化事件
	myWindow.Canvas().Focused()
	myWindow.Show()
	time.AfterFunc(300*time.Millisecond, func() {
		if FirstEntry != nil {
			fyne.Do(func() {
				//fixIME(myWindow, FirstEntry)
				//fixIME(myWindow, SecondEntry)
				myWindow.Canvas().Focus(FirstEntry)
			})
		}
	})

	//// 定义自定义快捷键 Ctrl+Shift+H
	//toggleShortcut := &desktop.CustomShortcut{
	//	KeyName:  fyne.KeyH,                                       // 快捷键的主键
	//	Modifier: fyne.KeyModifierControl | fyne.KeyModifierShift, // 组合键 Ctrl+Shift
	//}
	//
	//// **正确方法：使用 Canvas().AddShortcut() 添加全局快捷键**
	//myWindow.Canvas().AddShortcut(toggleShortcut, func(shortcut fyne.Shortcut) {
	//	// **正确方法：使用 Hidden() 检查窗口是否隐藏**
	//	myWindow.Hide()
	//	myWindow.Show()
	//	if myWindow.Hiden() {
	//		w.Show() // 如果窗口是隐藏的，则显示它
	//		contentLabel.SetText("Window is now visible. Press Ctrl+Shift+H to hide.")
	//	} else {
	//		w.Hide() // 如果窗口是显示的，则隐藏它
	//		contentLabel.SetText("Window is hidden. Press Ctrl+Shift+H to show.")
	//	}
	//})

	myApp.Run()
}

var (
	user32                  = syscall.NewLazyDLL("user32.dll")
	setCaretPos             = user32.NewProc("SetCaretPos")
	immGetContext           = user32.NewProc("ImmGetContext")
	immSetCompositionWindow = user32.NewProc("ImmSetCompositionWindow")
)

func fixIME(window fyne.Window, entry *widget.Entry) {
	hwnd := getWindowHandle(window)
	if hwnd == 0 {
		return
	}

	// 获取输入框位置
	pos := fyne.CurrentApp().Driver().AbsolutePositionForObject(entry)
	size := entry.Size()

	// 设置输入法窗口位置
	x, y := int32(pos.X), int32(pos.Y+size.Height)
	setCaretPos.Call(uintptr(x), uintptr(y))

	// 设置输入法组合窗口位置
	hIMC, _, _ := immGetContext.Call(uintptr(hwnd))
	if hIMC != 0 {
		var form compositionForm
		form.dwStyle = 1 // CFS_POINT
		form.ptCurrentPos.x = x
		form.ptCurrentPos.y = y
		immSetCompositionWindow.Call(hIMC, uintptr(unsafe.Pointer(&form)))
	}
}

func getWindowHandle(window fyne.Window) uintptr {
	// 获取窗口句柄的具体实现取决于您的驱动
	// 这里是一个示例实现
	return 0 // 实际应用中需要替换为真实句柄
}

type compositionForm struct {
	dwStyle      uint32
	ptCurrentPos struct {
		x int32
		y int32
	}
	rcArea uintptr
}
